package com.nisco.dms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nisco.dms.entity.Role;
import com.nisco.dms.repository.RoleDao;
import com.nisco.dms.shiro.ShiroDbRealm;
import com.nisco.dms.util.dwz.Page;
import com.nisco.dms.util.dwz.PageUtils;

/**
 * 
 * @author joey
 * 
 */
@Component
@Transactional(readOnly = true)
public class RoleService extends BaseService<Role, String> {
	private RoleDao roleDao;
	private ShiroDbRealm shiroRealm;

	public RoleService() {

	}

	@Autowired
	public RoleService(RoleDao roleDao) {
		super((JpaRepository<Role, String>) roleDao);
		this.roleDao = roleDao;
	}

	@Transactional
	public void update(Role role) {
		roleDao.save(role);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}

	@Transactional
	public void delete(String id) {
		roleDao.delete(id);
		shiroRealm.clearAllCachedAuthorizationInfo();
	}

	public List<Role> find(Page page, String name) {
		org.springframework.data.domain.Page<Role> roles = (org.springframework.data.domain.Page<Role>) roleDao.findByStrNameContaining(name, PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, roles);
		return roles.getContent();
	}

	@Autowired
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Autowired
	public void setShiroRealm(ShiroDbRealm shiroRealm) {
		this.shiroRealm = shiroRealm;
	}

}
